#!/usr/bin/python
#
# Main file for go-or-notgo
#

import jinja2
import os
import random
import time
import webapp2

#
# Sets up template directory
#
jinja_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(
        os.path.join(os.path.dirname(__file__), 'templates')))

#
# Main body of go-or-notgo
#
def decide():
    go = 0
    notgo = 0
    n = 4
    while (n > 0):
        random.seed(time.time() + n)
        res = 0
        for i in range(10):
            res += int(10 * random.random()) % 2
        if 0 == res%2:
            go += 1
        else:
            notgo += 1

        n -= 1

    return go > notgo


#
# Request handlers
#

class MainPage(webapp2.RequestHandler):
    """Main page / landing page handler"""
    def get(self):
        template = jinja_env.get_template('main.html')
        self.response.out.write(template.render({}))

class AskPage(webapp2.RequestHandler):
    """Ask page handler"""
    def get(self):
        template = jinja_env.get_template('ask.html')
        template_vars = {
            'goornot': decide()
        }
        self.response.out.write(template.render(template_vars))


app = webapp2.WSGIApplication(
    [('/', MainPage), ('/ask', AskPage)],
    debug=False)
